package com.example.cart;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductDetailActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	  setContentView(R.layout.activity_product_detail);
    	  
    	  List<Product> catalog = ProductHelper.getCatalog();
    	  
    	  int productIndex = getIntent().getExtras().getInt(ProductHelper.PRODUCT_INDEX);
    	  final Product selectedProduct = catalog.get(productIndex);
    	  final List<Product> cart = ProductHelper.getCart();
    	  // Set the proper image and text
    	  ImageView productImageView = (ImageView) findViewById(R.id.Image);
    	  Integer[] listImage = ProductAdapter.imgid;
    	  Integer indexImage = listImage[selectedProduct.getImageNumber()];
    	  productImageView.setImageResource(indexImage);
    	  TextView productTitleTextView = (TextView) findViewById(R.id.Name);
    	  productTitleTextView.setText(selectedProduct.getName());
    	  TextView productPriceTextView = (TextView) findViewById(R.id.Price);
    	  productPriceTextView.setText(selectedProduct.getPrice());
    	  TextView productDetailsTextView = (TextView) findViewById(R.id.Description);
    	  productDetailsTextView.setText(selectedProduct.getItemDescription());
    	  
    	  Button addToCartButton = (Button) findViewById(R.id.ButtonAddToCart);
    	  addToCartButton.setOnClickListener(new OnClickListener() {
    	   
    	   public void onClick(View v) {
    	    cart.add(selectedProduct);
    	    selectedProduct.setSelected(true);
    	    finish();
    	   }
    	  });
    	  
    	  // Disable the add to cart button if the item is already in the cart
    	  if(cart.contains(selectedProduct)) {
    	   addToCartButton.setEnabled(false);
    	   addToCartButton.setText("Item in Cart");
    	  }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_product_detail, menu);
        return true;
    }
}
